<?php
load_ctl("Common");
class ArticleController extends CommonController
{ 
	function init(){
		parent::init();
		load_model('Article');
		$this->article = new Article();
		load_model('Comment');
		$this->comment = new Comment();
		load_model('Class');
		$this->class = new APclass();
    }
	public function indexAction(){
		if(isset($_GET["id"]) && is_numeric($_GET["id"])){
			$id = intval($_GET["id"]);
		}else{
			$this->_redirect('/');
		}
		//更新点击数
		$this->article->update_count($id);
		$page = isset($_GET["p"]) ? intval($_GET["p"]) : 1;
	    $page < 1 && $page = 1;
		$page_size = 10;
		$this->view->article_info = $this->article->get_article_info($id);
		$this->view->art_comment = $this->comment->get_comment_by_art($id);
		$this->view->total_comment = count($this->view->art_comment);
		
		$this->view->webTitle = $this->view->article_info["art_title"].' - ';
		$this->view->keywords = $this->view->article_info["art_title"].','.$this->view->article_info["art_keyword"];
		$this->view->description = $this->view->article_info["art_title"];
		
		$paginator = Zend_Paginator::factory($this->view->art_comment); 
		$paginator->setCurrentPageNumber($page);
		$paginator->setItemCountPerPage($page_size); 
		$this->view->paginator = $paginator;
		echo $this->view->render('article.phtml');        
	}

	public function addcommentAction(){
		if($this->_request->isPost()){
			$filter = new Zend_Filter_StripTags();
			if(trim($this->_request->getPost('com_writer')) == ''){
				php_alert("用户名不能为空",-1);
				exit;
			}elseif(trim($this->_request->getPost('com_content')) == ''){
				php_alert("内容不能为空",-1);
				exit;
			}else if($this->_request->getPost('verify')=='' || $this->_request->getPost('verify') != $_SESSION['authcode']){
				php_alert("验证码出错",-1);
				exit;
			}
			$data = array(
				'com_art_id'=>$filter->filter($this->_request->getPost('id')),
				'com_pho_id'=>null,
				'com_writer'=>$filter->filter($this->_request->getPost('com_writer')),
				'com_content'=>$filter->filter($this->_request->getPost('com_content')),
				'com_date'=>time()
			);
			$this->comment->add_comment($data);
			$this->article->update_com_count($this->_request->getPost('id'),'add');//更新文章评论次数
			php_alert("评论发表成功",-1);
		}else{
			$this->_redirect('/');
		}
	}
	
	public function noRouteAction(){
		$this->_redirect('/');
	}
}
?>